package resolve

import (
	"log"

	"google.golang.org/grpc/resolver"
)

var severAddress = []string{
	":50051", ":50052", ":50053",
}

type Builder struct{}

func (b *Builder) Build(target resolver.Target, cc resolver.ClientConn, opts resolver.BuildOptions) (resolver.Resolver, error) {
	simResolver := &simpleResolver{cc: cc}
	simResolver.start()

	return simResolver, nil
}

func (b *Builder) Scheme() string {
	return "chris"
}

type simpleResolver struct {
	cc resolver.ClientConn
}

func (s *simpleResolver) start() {
	addrs := make([]resolver.Address, len(severAddress))
	for i, address := range severAddress {
		addrs[i] = resolver.Address{
			Addr: address,
		}
	}
	if err := s.cc.UpdateState(resolver.State{
		Addresses: addrs,
	}); err != nil {
		log.Fatal(err)
	}
}

// It could be called multiple times concurrently.
func (s *simpleResolver) ResolveNow(resolver.ResolveNowOptions) {}

// Close closes the resolver.
func (s *simpleResolver) Close() {}
